/*
 *  Copyright 2009 Lucas Nazário dos Santos
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.sourceforge.retriever.sample;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import net.sourceforge.retriever.Retriever;
import net.sourceforge.retriever.analyzer.Analyzer;
import net.sourceforge.retriever.fetcher.Resource;
import net.sourceforge.retriever.filter.URLRegexFilter;

/**
 * Defines an interval of 4 seconds between HTTP connections to servers with
 * the same IP.
 */
public class CrawlingHTTPResourcesWithIntervalBetweenConnections {


	public static void main(final String[] args) throws MalformedURLException {

		final Retriever retriever = new Retriever();

		retriever.addSeed(new URL("http://portal.stela.org.br/"));

		// Sets the interval of 4 seconds between connections to the same IP.
		retriever.getFrontier().setIntervalBetweenFetchesInMillis(4000);

		// Restricts the crawler to the domain stela.org.br
		final URLRegexFilter filter = new URLRegexFilter();
		filter.addRegex(".+portal\\.stela\\.org\\.br.+");
		retriever.setFilter(filter);

		// Prints out the URL of each crawled resource
		retriever.setAnalyzer(new Analyzer() {
			int i = 0;

			public void analyze(final Resource resource, final Map<String, Object> additionalInfo) {
				System.out.println(++i + " - " + resource.getURL().toExternalForm());
			}
		});

		retriever.start();
	}
}